package com.me.forum.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.enums.RespType;
import com.me.forum.domain.Remark;
import com.me.forum.request.RemarkRequest;
import com.me.forum.service.impl.RemarkService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/remark")
public class AdminRemarkController {
    // by Orange

    @Autowired
    private RemarkService remarkService;

    //根据id查询评论
    @GetMapping("/{id}")
    public IModel getById(@PathVariable String id, IModel model) {
        Remark remark = remarkService.findById(id);
        model.success(remark!=null);
        model.add(RespType.Remark, remark);
        return model;
    }

    //条件获取 Get
    @GetMapping
    public IModel getAllRemark(RemarkRequest rreq, IModel model,String page,String size) {
        IPage<Remark> remarks = remarkService.getRemarks(rreq,page,size);
        model.success(remarks!= null && remarks.getSize() != 0);
        model.add(RespType.Remarks, remarks);
        return model;
    }

    //新添加评论 Post
    @PostMapping
    public IModel addRemark(@RequestBody RemarkRequest rreq, IModel model) {
        model.success(remarkService.addRemark(rreq));
        return model;
    }
    //修改信息 Put
    @PutMapping
    public IModel updateRemark(@RequestBody RemarkRequest rreq, IModel model) {
        model.success(remarkService.updateById(rreq));
        return model;
    }
    //删除信息 Delete
    @DeleteMapping
    public IModel deleteRemark(String id, IModel model) {
        model.success(remarkService.delete(id));
        return model;
    }

}
